//
//  SOSViewController.m
//  location
//
//  Created by Dongxiang Xu on 2025/5/29.
//

#import "SOSViewController.h"
#import "AddSOSViewController.h"

@interface SOSViewController ()

@property (nonatomic,strong) UIScrollView *scrollView;

@end

@implementation SOSViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = RealWhiteColor;
    [self initView];
}

- (void)initView
{
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, STATUSHEIGHT, SCREEN_WIDTH, 44)];
    title.font = MEDIUM_FONT(18);
    title.textAlignment = NSTextAlignmentCenter;
    title.text = @"一键SOS";
    title.textColor = RealBlackColor;
    [self.view addSubview:title];
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, NAVHEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT-NAVHEIGHT)];
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_scrollView];
    
    UILabel *sos = [[UILabel alloc] initWithFrame:CGRectMake(28, 28, SCREEN_WIDTH-56, 20)];
    sos.font = MEDIUM_FONT(16);
    sos.textColor = RealBlackColor;
    sos.text = @"添加紧急联系人";
    [_scrollView addSubview:sos];
    
    UILabel *sosLabel = [[UILabel alloc] initWithFrame:CGRectMake(28, CGRectGetMaxY(sos.frame)+4, SCREEN_WIDTH-56, 16)];
    sosLabel.font = DEFAULT_FONT(12);
    sosLabel.textColor = GraryHeavyLightColor;
    sosLabel.text = @"与其共享位置，便于紧急情况快速呼救";
    [_scrollView addSubview:sosLabel];
    
    
    NSArray *array = @[@{},@{@"avatar":@"",@"nickname":@"张大千"},@{@"avatar":@"",@"nickname":@"泽连斯基"},@{@"avatar":@"",@"nickname":@"布莱恩特"},@{@"avatar":@"",@"nickname":@"罗纳尔多"}];
    NSInteger n = (array.count+1)%4==0?(array.count+1)/4:((array.count+1)/4+1);
    for (int i=0; i<n; i++) {
        for (int j=0; j<4; j++) {
            if ((j+4*i) < array.count) {
                UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(28+j*((SCREEN_WIDTH-56-62*4)/3+62), CGRectGetMaxY(sosLabel.frame)+12+100*i, 62, 86)];
                [_scrollView addSubview:button];                
                [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
                    if (i==0 && j==0) {
                        AddSOSViewController *vc = [[AddSOSViewController alloc] init];
                        [self.navigationController pushViewController:vc animated:YES];
                    }
                }];
                
                UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, button.width, button.width)];
                img.backgroundColor = GraryLightColor;
                img.layer.masksToBounds = YES;
                [img.layer setCornerRadius:12];
                [button addSubview:img];
                if (i==0 && j==0) {
                    img.image = [UIImage imageNamed:@"add_sos"];
                }else{
                    UIImageView *delete = [[UIImageView alloc] initWithFrame:CGRectMake(img.width-20, 0, 20, 20)];
                    delete.image = [UIImage imageNamed:@"delete_sos"];
                    [button addSubview:delete];
                    
                    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(img.frame)+6, button.width, 18)];
                    label.textColor = RealBlackColor;
                    label.font = MEDIUM_FONT(14);
                    label.textAlignment = NSTextAlignmentCenter;
                    label.text = array[j+4*i][@"nickname"];
                    [button addSubview:label];
                }
                if ((j+4*i)==(array.count-1)) {
                    [_scrollView setContentSize:CGSizeMake(0, CGRectGetMaxY(button.frame)+32)];
                }
            }
        }
    }
}

@end
